$(function () {
	var Accordion = function (el, multiple) {
		this.el = el || {};
		this.multiple = multiple || false;
		
		// Variables privadas
		var links = this.el.find('.link');
		// Evento
		links.on('click', {el: this.el, multiple: this.multiple}, this.dropdown)
	};
	
	Accordion.prototype.dropdown = function (e) {
		var $el = e.data.el;
			$this = $(this),
			$next = $this.next();
		
		$next.slideToggle();
		$this.parent().toggleClass('open');
		
		if (!e.data.multiple) {
			$el.find('.submenu').not($next).slideUp().parent().removeClass('open');
		}
	};
	
	var accordion = new Accordion($('#accordion'), false);
});

/**
 * 返回固定位数的字符串,不足位数以前缀prefix填充。
 * @param
 * @return
 * @author cjw 2019-08-08
 */
function truncateStr(inputStr, returnLength, prefix) {
	var returnStr = Array(returnLength + 1).join(prefix) + inputStr;
	returnStr = returnStr.slice(-returnLength);
	return returnStr;
}

/**
 * 渲染日期字段 开始日期《=结束日期。
 * @param
 * @return
 * @author cjw 2019-08-19
 */
function renderLaydate_startEndDate(startDateId,endDateId,type,format,isReadonly) {
	//分析输入的参数
	if(startDateId==null||endDateId==null){
		return;
	}
	if(type==null){
		type="date";
	}
	if(format==null){
		format="yyyy-MM-dd";
	}
	if(isReadonly==null){
		isReadonly=false;
	}
	
	//渲染开始时间
	var startDate = laydate.render({
		elem: '#' + startDateId, //通过id绑定
		type: type,
		format: format,
		min: "1900-01-01", //默认最小值
		max: "2099-12-31", //默认最大值
		done: function (value, dates) {
			//若dates不含有信息
			if (JSON.stringify(dates) == '{}') {
				endDate.config.min = {
					year: 1900,
					month: 0,//关键
					date: 1,
					hours: 0,
					minutes: 0,
					seconds: 0
				};
				return;
			}
			//若dates含有信息
			endDate.config.min = {
				year: dates.year,
				month: dates.month - 1, //关键
				date: dates.date,
				hours: dates.hours,
				minutes: dates.minutes,
				seconds: dates.seconds
			};
		}
	});
	//渲染结束时间
	var endDate = laydate.render({
		elem: '#' + endDateId, //通过id绑定
		type: type,
		format: format,
		min: "1900-01-01", //默认最小值
		max: "2099-12-31", //默认最大值
		done: function (value, dates) {
			//若dates不含有信息
			if (JSON.stringify(dates) == '{}') {
				startDate.config.max = {
					year: 2099,
					month: 11,//关键
					date: 31,
					hours: 0,
					minutes: 0,
					seconds: 0
				};
				return;
			}
			//若dates含有信息
			startDate.config.max = {
				year: dates.year,
				month: dates.month - 1,//关键
				date: dates.date,
				hours: dates.hours,
				minutes: dates.minutes,
				seconds: dates.seconds
			};
		}
	});
	
	//设置autocomplete
	$("#"+startDateId).attr("autocomplete","off");
	$("#"+endDateId).attr("autocomplete","off");
	//设置readonly
	if(isReadonly==true){
		$("#"+startDateId).attr("readonly",true).addClass("bg-color-white");
		$("#"+endDateId).attr("readonly",true).addClass("bg-color-white");
	}
}

/**
 * 渲染日期字段，dateIds为数组。
 * @param
 * @return
 * @author cjw 2019-08-19
 */
function renderLaydate(dateIds,type,format,isReadonly) {
	//分析输入的参数
	if (dateIds == null) {
		return;
	}
	if(type==null){
		type="date";
	}
	if(format==null){
		format="yyyy-MM-dd";
	}
	if(isReadonly==null){
		isReadonly=false;
	}
	
	//渲染日期字段
	for (var i = 0; i < dateIds.length; i++) {
		laydate.render({
			elem: '#' + dateIds[i],
			type: type,
			format: format
		});
	}
	
	//设置autocomplete,readonly
	for(var i=0;i<dateIds.length;i++){
		$("#"+dateIds[i]).attr("autocomplete","off");
		if(isReadonly==true){
			$("#"+dateIds[i]).attr("readonly",true).addClass("bg-color-white");
		}
	}
}

